package cn.lfzj.controller;

import java.util.List;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.lfzj.model.Order;
import cn.lfzj.model.User;
import cn.lfzj.service.OrderService;
import cn.lfzj.service.UserService;

@CrossOrigin
@Controller
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private UserService userService;

	/**
	 * 订单查询(多条件)
	 */
	@RequestMapping("showOrder")
	public String selAllOrder(Integer pageNum, Integer pageSize, Model model, String orderId, String username,
			String data, String startSta, String endSta, String orderState) {
		pageNum = pageNum == null || pageNum <= 0 ? 1 : pageNum;
		pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
		PageHelper.startPage(pageNum, pageSize);
		List<Order> list;
		if (username == null || username == "") {
			list = orderService.selAllOrder(orderId, 0, data, startSta, endSta, orderState);
		} else {
			list = orderService.selAllOrder(orderId, userService.selIdByName(username), data, startSta, endSta,
					orderState);
		}
		PageInfo<Order> pageInfo = new PageInfo<Order>(list);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("OrderList", list);
		model.addAttribute("startStations", startSta);
		model.addAttribute("endStations", endSta);
		model.addAttribute("orderId", orderId);
		model.addAttribute("data", data);
		return "manager/showOrder";
	}

	/**
	 * 订单删除
	 */
	@RequestMapping("del")
	public String del(String orderId) {
		orderService.delOrder(orderId);
		return "redirect:/order/showOrder";
	}

	/**
	 * 用户下单
	 */
	@ResponseBody
	@RequestMapping("createOrder")
	public String createOrder(Order order, User user) {
		JSONObject json = new JSONObject();
		order.setUser(user);
		String orderId = System.currentTimeMillis() + RandomStringUtils.random(7, false, true);
		order.setOrderId(orderId);
		if (orderService.addOrder(order) == 1) {
			json.put("code", 1);
			json.put("orderId", orderId);
		} else {
			json.put("code", 0);
		}
		return json.toJSONString();
	}

	/**
	 * 用户支付后更改订单状态
	 */
	@ResponseBody
	@RequestMapping("pay")
	public String pay(String orderId, int userId, double amount) {
		JSONObject json = new JSONObject();
		int code = 1;
		try {
			code = orderService.updateState(amount, orderId, userId);
		} catch (Exception e) {
			code = 2;
		}
		json.put("code", code);
		return json.toJSONString();
	}

	/**
	 * 用户端查询订单
	 */
	@ResponseBody
	@RequestMapping(value = "selOrder", produces = "text/plain;charset=utf-8")
	public String selByUserId(int userId) {
		JSONObject json = new JSONObject();
		json.put("order", orderService.selByUserId(userId));
		return json.toJSONString();
	}
}
